package com.sywb.chuangyebao.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.sywb.chuangyebao.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 录制视频进度条
 * Created by duxl on 2018/5/23.
 */

public class RECProgressView extends View {

    protected int mMaxProgress;
    protected int mMinProgress;
    protected int mCurrentProgress;
    protected int mProgressColor;
    protected int mPointColor;
    protected int mPointWidth;

    protected List<Integer> mPoints;

    protected Paint mPaint;

    public RECProgressView(Context context) {
        super(context);
        init();
    }

    public RECProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RECProgressView);
        mMaxProgress = typedArray.getInt(R.styleable.RECProgressView_maxProgress, 100);
        mMinProgress = typedArray.getInt(R.styleable.RECProgressView_minProgress, 0);
        mCurrentProgress = typedArray.getInt(R.styleable.RECProgressView_currentProgress, 0);
        mProgressColor = typedArray.getColor(R.styleable.RECProgressView_progressColor, Color.GRAY);
        mPointColor = typedArray.getColor(R.styleable.RECProgressView_pointColor, Color.TRANSPARENT);
        mPointWidth = typedArray.getDimensionPixelSize(R.styleable.RECProgressView_pointWidth, 0);
        typedArray.recycle();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPoints = new ArrayList<>();
    }

    public void addPoint(int point) {
        if(point <= mCurrentProgress && point <= mMaxProgress) {
            for (Integer p : mPoints) {
                if(p.intValue() == point) {
                    mPoints.remove(p);
                }
            }
            mPoints.add(point);
            postInvalidate();
        }
    }

    /**
     * 删除最后一个节点
     */
    public void deletePoint() {
        if(mPoints.size() == 1) {
            setProgress(0);
        } else if(mPoints.size() >= 2) {
            setProgress(mPoints.get(mPoints.size()-2).intValue());
        }
    }

    public List<Integer> getPoints() {
        return mPoints;
    }

    public void setMaxProgress(int max) {
        this.mMaxProgress = max;
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMinProgress(int min) {
        this.mMinProgress = min;
    }

    public int getMinProgress() {
        return mMinProgress;
    }

    public void setProgress(int progress) {
        if(progress > mMaxProgress) {
            return;
        }

        this.mCurrentProgress = progress;
        for (Integer p : mPoints) {
            if(p.intValue() > mCurrentProgress) {
                mPoints.remove(p);
            }
        }
        postInvalidate();
    }

    public int getProgress() {
        return mCurrentProgress;
    }

    public void setColor(int color) {
        this.mProgressColor = color;
    }

    public int getColor() {
        return mProgressColor;
    }

    public void setPointColor(int color) {
        this.mPointColor = color;
    }

    public int getPointColor() {
        return mPointColor;
    }

    public void setPointWidth(int width) {
        this.mPointWidth = width;
    }

    public int getPointWidth() {
        return mPointWidth;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void log(String msg) {
        //System.out.println("RECProgressView." + msg);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawProgress(canvas);
        drawMinPoint(canvas);
        drawPoints(canvas);
    }

    protected int getProgressWidth(int progress) {
        return  (getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) * progress / mMaxProgress;
    }

    protected void drawProgress(Canvas canvas) {
        mPaint.setColor(mProgressColor);
        Rect rect = new Rect(getPaddingLeft(), getPaddingTop(), getProgressWidth(mCurrentProgress), getBottom()-getPaddingBottom());
        canvas.drawRect(rect, mPaint);
        log("drawProgress: " + rect);
    }

    protected void drawMinPoint(Canvas canvas) {
        if(mMinProgress > 0 && mCurrentProgress < mMinProgress) {
            drawPoint(canvas, mMinProgress);
        }
    }

    protected void drawPoints(Canvas canvas) {
        for (Integer p : mPoints) {
            drawPoint(canvas, p.intValue());
        }
    }

    protected void drawPoint(Canvas canvas, int point) {
        if(point > 0 && point < mMaxProgress) {
            mPaint.setColor(mPointColor);
            int left = getProgressWidth(point) - mPointWidth;
            Rect rect = new Rect(left, getPaddingTop(), left + mPointWidth, getBottom()-getPaddingBottom());
            canvas.drawRect(rect, mPaint);
        }
    }
}
